package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.DayInfo;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.service.AdminService;
import com.example.kehoobackend.service.DayInfoService;
import com.example.kehoobackend.service.TopicService;
import com.example.kehoobackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Validated
@CrossOrigin
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private DayInfoService dayInfoService;

    @Autowired
    private TopicService topicService;

    /**
     * 管理员登录
     *
     * @param username 用户名
     * @param password 密码
     * @return CommonResponse<Map>
     */
    @GetMapping("/admin")
    public CommonResponse<Map> adminLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        return adminService.login(username, password);
    }

    /**
     * 管理员获取信息
     *
     * @return CommonResponse<Map < String, String>>
     */
    @GetMapping("/admin/info")
    public CommonResponse<Map<String, String>> getAdminInfo() {
        return adminService.getAdminInfo(1);
    }

    /**
     * 获取所有用户信息
     *
     * @return CommonResponse<List < User>>
     */
    @GetMapping("/admin/user")
    public CommonResponse<List<User>> getAllUserInfo() {
        return userService.getAllUserInfo();
    }


    /**
     * 筛选用户
     *
     * @param type 筛选类型(0-性别，1-用户名，2-所在地，3-职业)
     * @param value 筛选值
     * @return CommonResponse<List < User>>
     */
    @GetMapping("/admin/user/filter")
    public CommonResponse<List<User>> filterUser(
            @RequestParam("type") Integer type,
            @RequestParam("value") String value) {
        return userService.filterUser(type, value);
    }

    /**
     * 获得近7日问题，回答，用户增长情况
     *
     * @return CommonResponse<List<DayInfo>>
     */
    @GetMapping("/admin/dayInfo")
    public CommonResponse<List<DayInfo>> getDayInfo() {
        return CommonResponse.createForSuccess("近日的数据",dayInfoService.getRecentDayInfo());
    }

    /**
     * 获取与话题相关的数据
     *
     * @return CommonResponse<List<Map < String, Object>>>  key为话题名字，value为话题包含的问题数
     */
    @GetMapping("/admin/topic")
    public CommonResponse<List<Map<String, Object>>> getTopicInfo() {
        return topicService.getQuestionCountsByTopicName();
    }





    /**
     * 将指定用户的密码重置为123456
     *
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/admin/{user_id}/password")
    public CommonResponse<Boolean> resetUserPassword(
            @PathVariable("user_id") Integer user_id) {
        return userService.resetUserPassword(user_id)
                ? CommonResponse.createForSuccess("重置成功", true)
                : CommonResponse.createForSuccess("重置失败", false);
    }

    /**
     * 更新上次登录时间
     *
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/admin/time")
    public CommonResponse<Boolean> updateLastLoginTime() {
        return adminService.updateLastLoginTime()
                ? CommonResponse.createForSuccess("更新成功", true)
                : CommonResponse.createForSuccess("更新失败", false);
    }





    /**
     * 管理员登录
     *
     * @param admin_id 管理员id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/admin")
    public CommonResponse<Boolean> adminLogout(
            @RequestParam("admin_id") int admin_id) {
        return adminService.logout(admin_id)
                ? CommonResponse.createForSuccess("退出成功", true)
                : CommonResponse.createForSuccess("退出失败", false);
    }
}
